
import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs';

import { environment } from '../environments/environment';
import { Department } from '../models/Department';

@Injectable()
export class DepartmentService {

  private baseUrl =  environment.apiUrl + 'api/sysdepartment';


  constructor(
        private http: HttpClient
  ) {}

  getAllDepartments(): Observable<{res: Department[], success: boolean, total: number}> {
    return this.http.get<{res: Department[], success: boolean, total: number}>(`${this.baseUrl}/getlist`);
  }

  getDepartments(current: number = 1, pageSize: number = environment.pageSize):
    Observable<{res: Department[], success: boolean, total: number}> {
      let params: HttpParams = new HttpParams();
      params = params.append('page', String(current));
      params = params.append('pageSize', String(pageSize));
      return this.http.get<{res: Department[], success: boolean, total: number}>(`${this.baseUrl}/getlist`, { params : params});
  }

    getDepartment(id: string): Observable<{res: Department, success: boolean}> {
        return this.http.get<{res: Department, success: boolean}>(`${this.baseUrl}/getbyid/${id}`);
    }

    update(department: Department): Observable<{res: Department, success: boolean}> {
        const url = `${this.baseUrl}/update`;
        return this.http
            .post<{res: Department, success: boolean}>(url, department);
    }

    create(department: Department): Observable<{success: boolean, res: Department}> {
        return this.http
            .post<{success: boolean, res: Department}>(`${this.baseUrl}/insert`, department);
    }

    del(id: number): Observable<{success: boolean}> {
        const url = `${this.baseUrl}/del/${id}`;
        return this.http.get<{success: boolean}>(url, {});
    }


}
